<?php
/**
 * 微信公众平台 PHP SDK 示例文件
 *
 * @author NetPuter <netputer@gmail.com>
 */
require('wechat.php');
//默认回复
require_once('../common/http_helper.php');
require_once('../common/weixin_helper.php');
require_once('../common/function.php');

/**
 * 微信公众平台演示类
 */
class MyWechat extends Wechat
{
    /**
     * 用户关注时触发，回复「欢迎关注」
     *
     * @return void
     */
    protected function welcome($veryfs = '')
    {
        $xuanzezu = $this->xuanzezu;
        $weixin_name = $this->weixin_name;
        $from = $this->getRequest('fromusername');
        // logechostr('welcome');
        global $plugsc;
        $plugs = $plugsc->select('switch =1');
        //签到不用手机号 判断是网页授权 还是微信基本信息接口


        //需要内定抽奖的用户才需要手机号

//        if ($xuanzezu['rentweixin'] == 1) {
//            $url = Web_ROOT . "/weixin/authorization.php?openid=" . $from;
//            $items = array(
//                new NewsResponseItem("欢迎使用{$weixin_name}",
//                    "点击获取您的信息，进行开场签到，使用上墙功能\n", Web_ROOT . "/files/images/qiandao.jpg",
//                    $url)
//            );
//        } else {
            if ($veryfs == '') {
                $items = array(
                    new NewsResponseItem("欢迎关注{$weixin_name}"),
                    new NewsResponseItem('【上墙】发送【' . $xuanzezu['huati'] . '任意内容】即可上墙'),
                    new NewsResponseItem('【图片】直接发送图片即可上墙'),
                );
                // logechostr('qiandao msg');
            } else {
                $url_qiandao = Web_ROOT . "/sign/index.php?openid=" . $from;
                $items = array(
                    //链接需要改成一个可以验证的地址
                    new NewsResponseItem("欢迎使用{$weixin_name}",
                        "点击获取您的信息进行开场签到\n", Web_ROOT . "/files/images/qiandao.jpg",
                        $url_qiandao)
                );

            }
//        }

        $flag_m = new M('flag');
        $q = $flag_m->find('openid="' . $from . '"');
        // logechostr('qiandao flag');
        // return $q;
        // // $this->check = $check;
        // logechostr($q['flag']);
        // logechostr('end');
        if ($q['flag'] == 2) {
            foreach ($plugs as $plugin) {
                $decripton = str_replace("~~[keyword]~~", $plugin['keyword'], $plugin['description']);
                array_push($items, new NewsResponseItem($decripton));
            }
        }
        // logechostr('welcome4');
        return $items;

    }

    /**
     * 验证码回复
     *
     * @return void
     */
    protected function VERYF($weixin_name, $veryfs)
    {
        $items = $this->welcome($veryfs);
        // logechostr('veryf');
        Wechat::$noendtail = 0;
        return $items;
    }

    /**
     * 用户关注时触发，回复 关注时候回重置
     *
     * @return void
     */
    protected function onSubscribe()
    {
        $weixin_name = $this->weixin_name;
        $from = $this->getRequest('fromusername');
        $check = $this->sqlselect('flag', $from, "row");
        $this->check = $check;
        $reply = $this->resetflag($weixin_name, $from);
        if (Wechat::$noendtail) {
            $reply = $this->endtail($reply);
        }

        $this->responseNews($reply);
    }

    /**
     * 用户已关注时,扫描带参数二维码时触发，回复二维码的EventKey (测试帐号似乎不能触发)
     *
     * @return void
     */
    protected function onScan()
    {
        $this->responseText('二维码的EventKey：' . $this->getRequest('EventKey'));
    }

    /**
     * 用户取消关注时触发
     *
     * @return void
     */
    protected function onUnsubscribe()
    {
        //$this->deleteflag($this->getRequest('fromusername'));
        // 「悄悄的我走了，正如我悄悄的来；我挥一挥衣袖，不带走一片云彩。」
    }

    /**
     * 收到图片消息时触发，回复由收到的图片组成的图文消息
     *
     * @return void
     */
    protected function onImage()
    {
        $xuanzezu = $this->xuanzezu;
        $weixin_name = $this->weixin_name;
        $picurl = makeimg($this->getRequest('picurl'), $xuanzezu['web_root']);
        $this->request['picurl'] = $picurl;
        $from = $this->getRequest('fromusername');
        $time = $this->getRequest('createtime');
//        $check = $this->sqlselect('flag', $from, "row");
        $flag_m = new M('flag');
        $check = $flag_m->find('openid="' . $from . '"');
        // if (!$check[1]) {
        if (!$check['openid']) {
            $reply = $this->resetflag($weixin_name, $from);
        }

        // if ($check[2] == '2') {
        if ($check['flag'] == '2') {
            $reply = $this->replaypic($from, $time, $picurl);

        } else {
            global $plugsc;
            // $plugs = $plugsc->find("switch=1 and status='" . $check[2] . "'");
            $plugs = $plugsc->find("switch=1 and status='" . $check['flag'] . "'");
            if (!empty($plugs)) {
                $classname = $plugs['name'] . "_plug";
                $replyc = new $classname($xuanzezu, $this->check, $this->request);
                $method = $plugs['name'] . '_replay';
                $reply = $replyc->$method();
            } else {
                $veryfs = '手机号';// $q['verify'];
                $reply = $this->VERYF($weixin_name, $veryfs);
            }
        }
        if ($xuanzezu['fusionopen'] && Wechat::$noendtail) {
            $end = new NewsResponseItem('回复【退出】退出本活动系统', '');
            array_push($reply, $end);
        }
        if (Wechat::$noendtail) {
            $reply = $this->endtail($reply);
        }
        $this->responseNews($reply);
    }

    /**
     * 收到文本消息时触发，回复收到的文本消息内容
     *
     * @return void
     */
    protected function onText()
    {

        $xuanzezu = $this->xuanzezu;
        $weixin_name = $this->weixin_name;

        $content = $this->getRequest('content');
        $from = $this->getRequest('fromusername');
        $time = $this->getRequest('createtime');
        $content = $this->biaoqing($content);

        //$check = $this->sqlselect('flag', $from, "row");
        $flag_m = new M('flag');
        $check = $flag_m->find('openid="' . $from . '"');
        //logechostr($check[2]);
        $this->check = $check;
        if ($content == '退出') {
            $this->deleteflag($from);
            $reply = array(new NewsResponseItem('您已成功退出本活动系统。', ''));
            $check['flag'] = '-10';
        }
        if ($content == '重置') {
            $reply = $this->resetflag($weixin_name, $from);
            $check['flag'] = '-10';
        }
        if ($content == '查询') {
            $this->findcj($check);
        }
        
        $autoreply_m=new M('autoreply');
        $autoreplylist=$autoreply_m->select('1');
        $autoreplymap=array();
        foreach($autoreplylist as $k=>$v){
            $autoreplymap[$v['message']]=$v;
        }
        if(isset($autoreplymap[$content])){
            // logechostr(var_export($autoreplymap,true));
                $reply = array(
            new NewsResponseItem($autoreplymap[$content]['title'],
                 $autoreplymap[$content]['description'], $autoreplymap[$content]['picurl'],
                 $autoreplymap[$content]['url'])
            );
            $this->responseNews($reply);
            return ;
        
        }

        // logechostr('openid' . $check['openid']);
        if (!$check['openid']) {
            $reply = $this->resetflag($weixin_name, $from);
        }
        //判断用户是否没有录入信息
        if ($check['flag'] == '1') {
            $veryfs = '手机号';
            $reply = $this->VERYF($weixin_name, $veryfs);
            if (checkMobile($content) > 0) {
                // logechostr('验证3');
                $reply = $this->monigetinfo($from, $time, $content, $veryfs);
            } else {
                // logechostr('验证2');
                $reply = $this->VERYF($weixin_name, $veryfs);
            }
        } elseif ($check['flag'] == '2') {
            $reply = $this->replay($from, $time, $content);
        } else {
            global $plugsc;
            $plugs = $plugsc->find("switch=1 and status='" . $check['flag'] . "'");
            if (!empty($plugs)) {
                $classname = $plugs['name'] . "_plug";
                $replyc = new $classname($xuanzezu, $this->check, $this->request);
                $method = $plugs['name'] . '_replay';
                $reply = $replyc->$method();
            }
        }

        if ($xuanzezu['fusionopen'] && Wechat::$noendtail) {
            // logechostr('fusionopen 1');
            $end = new NewsResponseItem('回复【退出】退出本活动系统', '');

            array_push($reply, $end);
            // logechostr(Wechat::$noendtail);
            // logechostr('fusionopen end');
            // $this->responseNews($reply);
            // return;
        }
        if ($check['flag'] != '88' && Wechat::$noendtail) {
            $reply = $this->endtail($reply);
        }
        $this->responseNews($reply);
    }

    /**
     * 信息录入
     *
     * @return void
     */
    protected function monigetinfo($from, $time, $content, $veryfs)
    {
        $isluru = isluru($from);//, $time, $content);
        // $reply = array(
        //         new NewsResponseItem("获取失败1"),
        //         new NewsResponseItem("抱歉，服务器繁忙，您的信息获取失败，请重新回复【{$veryfs}】！"),
        //     );
        // return $reply;
        if ($isluru < 0) {
            $reply = array(
                new NewsResponseItem("获取失败2"),
                new NewsResponseItem("抱歉，服务器繁忙，您的信息获取失败，请重新回复【{$veryfs}】！"),
            );
        } else {
            // $reply = array(
            //     new NewsResponseItem("获取失败3"),
            //     new NewsResponseItem("抱歉，服务器繁忙，您的信息获取失败，请重新回复【{$veryfs}】！"),
            // );
            // return $reply;

            $shady = $this->sqlselect('cj_shady', $content, "row", 'phone');
            $sql_check = '';
            if ($shady > 0) {
                $sql_check = "UPDATE  `weixin_flag` SET `flag` = '2',`phone` = '{$content}',`shady`='" . $shady['grade'] . "'  WHERE  `openid` =  '{$from}';";
                // mysql_query($sql_check);
            } else {
                $sql_check = "UPDATE  `weixin_flag` SET `flag` = '2',`phone` = '{$content}'  WHERE  `openid` =  '{$from}';";
                // mysql_query($sql_check);
            }
            // $sql_check = "UPDATE  `weixin_flag` SET `flag` = '2'  WHERE  `openid` =  '{$from}';";

            $sql_check_m = new M('flag');
            $sql_check_m->query($sql_check);
//            mysql_query($sql_check);
            //该地址为1.php上传后的地址
            $reply = $this->welcome();
        }
        return $reply;
    }

    /**
     * 信息录入之后的回复函数
     *
     * @return void
     */
    protected function replay($from, $time, $content)
    {

        $xuanzezu = $this->xuanzezu;
        $weixin_name = $this->weixin_name;
        if ($content == '帮助') {
            $reply = $this->welcome();
            $check[2] = '-10';
            return $reply;
        }
        /*话题判断函数开始*/
        function startsWith($haystack, $needle, $case = false)
        {
            if ($case) {
                return strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0;
            }
            return strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0;
        }

        /*话题判断函数结束*/
        /*屏蔽词判断*/
        function blackword($content, $xuanzezu)
        {
            if (!empty($xuanzezu['black_word'])) {
                $blackarr = explode(",", $xuanzezu['black_word']);

                foreach ($blackarr as $v) {
                    if (strstr($content, $v)) {
                        return 1;
                    }
                }
                return 0;
            }
        }

        global $plugsc;
        $plugs = $plugsc->find('switch=1 and needword = 1 and keyword="'.$content.'"');

        if (!empty($plugs)) {
            $classname = $plugs['name'] . "_plug";
            $replyc = new $classname($xuanzezu, $this->check, $this->request);
            $method = $plugs['name'] . '_replay';
            $reply = $replyc->$method();
        } else {
            if (startsWith($content, $xuanzezu['huati'])) {
                if (blackword($content, $xuanzezu)) {
                    $this->responseText('对不起，您发送的内容中有屏蔽的词语，上墙失败，请重新编辑后发送！');
                }
                $q = $this->sqlselect('flag', $from);
                // if ($this->check[4] != '') {
                if ($this->check['nickname'] != '') {
                    $reply = $this->writeinwall($content, $time, $q);
                } else {
                    $reply = $this->unkownerror();
                }
            } else {
                $reply = $this->welcome();
            }
        }

        return $reply;
    }

    /**
     * 写入wall函数
     *
     * @return void
     */
    protected function writeinwall($content, $time, $q, $type = '')
    {
        $xuanzezu = $this->xuanzezu;
        $weixin_name = $this->weixin_name;
        //时间间隔判断

        if (($time - $q['datetime']) < $xuanzezu['timeinterval']) {
            $reply = array(
                new NewsResponseItem("休息一下"),
                new NewsResponseItem("您发送的太快了，墙上还有位置慢慢来~"),
            );
            return $reply;
        }
        //以下为写入wall
        if ($type == 'pic') {
            $sql = "INSERT INTO `weixin_wall` (`id`,`messageid`,`fakeid`,`num`,`content`,`nickname`,`avatar`,`ret`,`fromtype`,`image`,`datetime`) VALUES (NULL,'0','{$q['fakeid']} ','-1','此消息为图片！','{$q['nickname']}','{$q['avatar']}','0','weixin','{$content}','{$time}')";
        } else {
            $content = bin2hex($content);
            $sql = "INSERT INTO `weixin_wall` (`id`,`messageid`,`fakeid`,`num`,`content`,`nickname`,`avatar`,`ret`,`fromtype`,`datetime`) VALUES (NULL,'0','{$q['fakeid']}','-1','{$content}','{$q['nickname']}','{$q['avatar']}','0','weixin','{$time}')";
        }
        $wall_m = new M('wall');
        $wall_m->query($sql);
//        mysql_query($sql);
        $q['nickname'] = pack('H*', $q['nickname']);
        $usernickname = new NewsResponseItem("亲爱的【{$q['nickname']}】");
        if ($type == 'pic') {
            $usernickname = new NewsResponseItem("亲爱的【{$q['nickname']}】", "这是图片消息", $content, $content);
        }

        if ($xuanzezu['shenghe'] == 0) {
            $row = $wall_m->find(1, 'max(id) as id');
            $maxid = $row['id'];
            doshenhe($maxid);
            $isauto = new NewsResponseItem("您的消息已经成功发送，请多关注墙上内容！");
        } else {
            $isauto = new NewsResponseItem("您的消息已经成功发送，等待管理员审核后即可上墙，请多关注墙上内容！");
        }
        $reply = array();
        
        array_unshift($reply, $usernickname, $isauto);
        return $reply;
    }

    /**
     * 收到地理位置消息时触发，回复收到的地理位置
     *
     * @return void
     */
    protected function onLocation()
    {
        //$num = 1 / 0;
        // 故意触发错误，用于演示调试功能

        $this->responseText();
    }

    /**
     * 收到链接消息时触发，回复收到的链接地址
     *
     * @return void
     */
    protected function onLink()
    {
        $this->responseText('收到了链接：' . $this->getRequest('url'));
    }

    /**
     * 收到语音消息时触发，回复语音识别结果(需要开通语音识别功能)
     *
     * @return void
     */
    protected function onVoice()
    {
        $this->responseText('请发送文字信息进行上墙');
    }

    /**
     * 收到自定义菜单消息时触发，回复菜单的EventKey
     *
     * @return void
     */
    protected function onClick()
    {
        logechostr('测试按键');
        $eventkey = $this->getRequest('eventkey');
        $this->request['content'] = $eventkey;
        $xuanzezu = $this->xuanzezu;
        $sql_check = "UPDATE  `weixin_flag` SET `flag` = '2'  WHERE  `flag` >  2;";
        $sql_check_m = new M('flag');
        $sql_check_m->query($sql_check);
        global $plugsc;
        $plugs = $plugsc->find('switch=1 and needword = 1 and keyword="'.$eventkey.'"');
        // logechostr('按键信息记录：'.$eventkey);
        if (!empty($plugs) || $eventkey == '帮助' || $eventkey == '上墙') {
            // logechostr('按键信息记录：'.$eventkey);
            $this->onText();
        } else {
            if ($xuanzezu['fusionopen']) {
                exit($this->sendxml($GLOBALS['HTTP_RAW_POST_DATA'], $xuanzezu['fusionurl'], $xuanzezu['fusiontoken']));
            }
        }
//        $this->responseNews($reply);
    }

    /**
     * 收到未知类型消息时触发，回复收到的消息类型
     *
     * @return void
     */
    protected function onUnknown()
    {
        $this->responseText('收到了未知类型消息：' . $this->getRequest('msgtype'));
    }


}

function makeimg($picurl, $webpath)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $picurl);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1521.3 Safari/537.36");
    $output = curl_exec($ch);
    curl_close($ch);
    if (!empty($_SERVER['HTTP_APPNAME'])) {
        $weburl = saeup($output);
    } else {
        $weburl = commenup($output, $webpath);
    }
    return $weburl;
}

function commenup($write, $webpath)
{
    $filename = "pic_" . time() . ".jpg";//要生成的图片名字
    $file = fopen("../img/pic/" . $filename, "w");//打开文件准备写入
    fwrite($file, $write);//写入
    fclose($file);//关闭
    $imgurl = $webpath . "/img/pic/" . $filename;
    return $imgurl;
}

function saeup($write)
{
    $filename = "pic_" . time() . ".jpg";//要生成的图片名字
    //以下为sae的sotrage
    $domain = 'img';
    $s = new SaeStorage();
    $imgurl = $s->write($domain, $filename, $write);
    return $imgurl;
}

$weixinconf = getWeixinConf();
$wechat = new MyWechat($weixinconf['token'], 'weixin', false);
$wechat->run();
